package cn.easy.kitchen.service;

import cn.easy.base.bean.PageBean;
import cn.easy.base.service.BaseService;
import cn.easy.base.utils.jdbc.PaginationHelper;
import cn.easy.kitchen.domain.StatMaterialStructure;
import cn.easy.kitchen.repository.StatMaterialStructureDao;
import cn.easy.kitchen.utils.ProjectUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class StatMaterialStructureService extends BaseService<StatMaterialStructure> {
    @Autowired
    private StatMaterialStructureDao	statMaterialStructureDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Page<StatMaterialStructure> search(Map<String, Object> searchParams, PageBean pageBean) {
        return statMaterialStructureDao.findAll(spec(searchParams), pageBean.toPageRequest(new Sort(Direction.DESC, "createdAt")));
    }

    public List<StatMaterialStructure> search(Map<String, Object> searchParams, Sort... sort) {
        return statMaterialStructureDao.findAll(spec(searchParams), sort == null || sort.length == 0  ? new Sort(Direction.DESC, "createdAt") : sort[0]);
    }

    public Page<StatMaterialStructure> searchByParams(Map<String, Object> searchParams, PageBean pageBean) {
        List<Object> args = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT DISTINCT cc.name as name, SUM(meat) as meat_sum, sum(vegetables) as vegetables_sum, sum(aquatic_Products) as aquatic_Products_sum ");
        sql.append("from ck_stat_material_structure sms ");
        sql.append("LEFT JOIN ck_customer cc on sms.customer_id = cc.id ");

        sql.append("where sms.data_on >= ? ");
        args.add(ProjectUtil.formatDate(searchParams.get("GTE_dataOn")));

        sql.append("and sms.data_on <= ? ");
        args.add(ProjectUtil.formatDate(searchParams.get("LTE_dataOn")));

        sql.append("and cc.name is not null ");
        sql.append("GROUP BY sms.customer_Id ");
        sql.append("order by (SUM(meat) + sum(vegetables)+ sum(aquatic_Products)) desc ");

        PaginationHelper<StatMaterialStructure> helper = new PaginationHelper<>();
        return helper.fetchPage(jdbcTemplate, sql.toString(), args.toArray(), pageBean, new RowMapper<StatMaterialStructure>() {
            @Override
            public StatMaterialStructure mapRow(ResultSet rs, int i) throws SQLException {
                StatMaterialStructure bean = new StatMaterialStructure();
                bean.setCustomerId(ProjectUtil.cutStrLength(rs.getString("name"), 6));
                bean.setMeat(rs.getBigDecimal("meat_sum"));
                bean.setVegetables(rs.getBigDecimal("vegetables_sum"));
                bean.setAquaticProducts(rs.getBigDecimal("aquatic_Products_sum"));
                return bean;
            }
        });
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(String id) {
        statMaterialStructureDao.delete(id);
    }

    public StatMaterialStructure getOne(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return statMaterialStructureDao.findOne(id);
    }

    public List<StatMaterialStructure> findAll(List<String> idList) {
        return Lists.newArrayList(statMaterialStructureDao.findAll(idList));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public StatMaterialStructure save(StatMaterialStructure statMaterialStructure) {
        return statMaterialStructureDao.save(statMaterialStructure);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void save(List<StatMaterialStructure> statMaterialStructureList) {
        statMaterialStructureDao.save(statMaterialStructureList);
    }
}


